﻿using System;
using System.Windows.Input;
using System.Windows.Media;

using MSAP.Framework.Interfaces;

namespace MSAP.Framework
{
    public class ServiceActionCommand : RelayCommand, IServiceAction
    {
        // ServiceActionCommand associé a un module 
        public ServiceActionCommand(string groupId, string id, string titre, string comments,
                                    Action<object> execute, Predicate<object> canExecute,
                                    bool visible, ImageSource icon, IModule module, string sRoleName)
            : base(execute, canExecute)
        {
            Constructor(groupId, id, titre, comments, execute, canExecute, visible, icon, module, null, sRoleName);
        }

        public ServiceActionCommand(string groupId, string id, string titre, string comments,
                                    Action<object> execute, Predicate<object> canExecute,
                                    bool visible, ImageSource icon, IModule module)
            : base(execute, canExecute)
        {
            Constructor(groupId, id, titre, comments, execute, canExecute, visible, icon, module,null, null);
        }


        public ServiceActionCommand(string groupId, string id, string titre, string comments,
                                    Action<object> execute, Predicate<object> canExecute,
                                    IModule module)
            : base(execute, canExecute)
        {
            Constructor(groupId, id, titre, comments, execute, canExecute, true, null, module, null, null);
        }
        
        // ServiceActionCommand associé à une ServicePage
        public ServiceActionCommand(string groupId, string id, string titre, string comments,
                                    IServicePage SP, string sRoleName)
        {
            Constructor(groupId, id, titre, comments, null, null, true, null, SP.Module, SP, sRoleName);
        }

        public ServiceActionCommand(string groupId, string id, string titre, string comments,
                                    IServicePage SP)
        {
            Constructor(groupId, id, titre, comments, null, null, true, null, SP.Module, SP, null);
        }

        protected void Constructor(string groupId, string id, string titre, string comments, 
                                    Action<object> execute, Predicate<object> canExecute,
                                    bool visible, ImageSource icon,IModule module,IServicePage SP,string sRoleAccess)
        {
            this.m_GroupID = groupId;
            this.m_ID = id;
            this.m_Titre = titre;
            this.m_Comments = comments;
            this.m_Icon = icon;
            this.m_Visible = visible;
            this.m_Module = module;
            this.m_ServicePage = SP;
            this.m_Shell = (module != null) ? module.Shell : null;
            this.m_RolesAccess = sRoleAccess;
        }

        #region IServiceAction Members

        public void Init(IModule _Module)
        {
            m_Module = _Module;
        }

        public IModule Module
        {
            get { return m_Module; }
        }

        public IServicePage ServicePage
        {
            get { return m_ServicePage; }
        }

        public IShell Shell
        {
            get { return m_Shell; }
        }

        public string ID
        {
            get { return m_ID; }
        }

        public string GroupID
        {
            get { return m_GroupID; }
        }

        public string Title
        {
            get { return m_Titre; }
        }

        public string Comments
        {
            get { return m_Comments; }
        }

        public ImageSource Icon
        {
            get { return m_Icon; }
        }

        public ICommand Command
        {
            get 
            { 
                return this; 
            }
        }

        public bool Visible
        {
            get { return m_Visible; }
        }

        public string RolesAccess
        {
            get { return m_RolesAccess; }
            set { m_RolesAccess = value; }
        }

        public bool CheckAccessForRole(string sRoleName)
        {
            if (string.IsNullOrEmpty(RolesAccess) == true) return true; // pas de role access donc par dfaut on a accés au module
            
            // rechercher dans la liste des roles access, si le role de l'utilisateur est present.
            // dans le cas ou le role de l'utilisateur est present, il a access à la fonction
            // Par defaut, il n'a pas acces
            string [] roleNames = RolesAccess.Split(',');
            foreach(string role in roleNames)
            {
                if (role.CompareTo(sRoleName) == 0)
                    return true;
            }

            return false;
        }

        #endregion

        #region private fields
        protected string m_ID;
        protected string m_GroupID;
        protected string m_Titre;
        protected string m_Comments;
        protected ImageSource m_Icon;
        protected bool m_Visible;
        protected string m_RolesAccess;
        protected IModule m_Module;
        protected IShell m_Shell;
        protected IServicePage m_ServicePage;
        #endregion
    }
}
